package com.bkgy.controller.allprocessRcl1630.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.allprocessRcl1630.dto.AllProcessTbYieldRcl1630Requst;
import com.bkgy.controller.allprocessRcl1630.entity.TbYieldRcl1630Export;
import com.bkgy.controller.allprocessRcl1630.mapper.ITbYieldRcl1630Mapper;
import com.bkgy.controller.allprocessRcl1630.mapper.Rcl1630ExportMapper;
import com.bkgy.controller.allprocessRcl1630.service.AllProcessRcl1630ExportService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class TbYieldRcl1630ExportImpl extends ServiceImpl<Rcl1630ExportMapper, TbYieldRcl1630Export> implements AllProcessRcl1630ExportService {
    @Resource
    private HttpServletResponse response;

    @Autowired
    ITbYieldRcl1630Mapper tbYieldRcl1630Mapper;


    @Override
    public void export1630(AllProcessTbYieldRcl1630Requst queryDTO)
    {
        //日期转换
        queryDTO.setDbegin(setdatetime(queryDTO.getDbegin()));
        queryDTO.setDend(setdatetime(queryDTO.getDend()));

        List<TbYieldRcl1630Export> dataList=tbYieldRcl1630Mapper.rcl1630detaisExport(queryDTO);
        try {
            // 设置导出参数
            ExportParams exportParams = new ExportParams("重卷1630明细表", "重卷1630明细表", ExcelType.XSSF);

            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, TbYieldRcl1630Export.class, dataList);

            // 行列固定
            if (workbook.getNumberOfSheets() > 0) {
                workbook.getSheetAt(0).createFreezePane(0, 0);
            }

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("重卷1630明细表", StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException("导出Excel失败", e);
        }
    }

    /**
     * 构造 时间格式：2025/09/01 00:00:01
     * @param times
     * @return time
     */
    public String setdatetime(String times) {
        String time = "2025/09/01 00:00:01";//时间格式：2025/09/01 00:00:01
        try {
            String[] array_startTime = times.split("-");//时间格式：2025-06-01 00:00:01
            time = array_startTime[0] + "/" + array_startTime[1] + "/" + array_startTime[2];
             } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }
}

